﻿using System;
using System.Collections.Generic;
using System.Text;
using WordService.Business.IBLL;
using WordService.Data.IDAL;
using WordService.Data;
using WordService.Model;

namespace WordService.Business.BLL
{
    public sealed class ArticleBLL : IArticleBLL
    {
        // 在创建文章的时候需要调用所有的接口，所有的模型都不需要重名检测
        private IArticleDAL articleDAL = DALManager.CreateArticleDAL();
        private IKeywordDAL keywordDAL = DALManager.CreateKeywordDAL();
        private IAuthorDAL authorDAL = DALManager.CreateAuthorDAL();
        private IFileBLL fillDAL = BLLManager.CreateFileBLL();
        private IInstitutionDAL insitutionDAL = DALManager.CreateInstitutionDAL();

        private Decimal CalculateRate(String name, Model.Article article)
        {
            Decimal rate = Decimal.Parse("0.0");
            IDictionary<String, Decimal> map = this.fillDAL.GetKeywordRate();
            // 中级权值计算
            bool summary = false;
            if (article.Summary.Trim().IndexOf(name.Trim()) > 0 &&
                article.Summary.Trim().LastIndexOf(name.Trim()) > 0 &&
                article.Summary.Trim().IndexOf(name.Trim()) < article.Summary.Trim().LastIndexOf(name.Trim()))
            {
                summary = true;
            }
            // 次中级权值
            bool middle = false;
            if (article.Summary.Trim().IndexOf(name.Trim()) > 0 &&
                article.Summary.Trim().LastIndexOf(name.Trim()) > 0 &&
                article.Summary.Trim().IndexOf(name.Trim()) == article.Summary.Trim().LastIndexOf(name.Trim()))
            {
                middle = true;
            }
            // 权值计算
            if (article.Title.Trim().Contains(name.Trim())
                && article.Summary.Trim().Contains(name.Trim()))
            {
                map.TryGetValue("SAME", out rate);
            }
            else if (article.Title.Trim().Contains(name.Trim()) ||
               summary)
            {
                map.TryGetValue("TITLE", out rate);
            }
            else if(!article.Title.Trim().Contains(name.Trim()) &&
                middle)
            {
                map.TryGetValue("SUMMARY", out rate);
            }
            else if (!article.Title.Trim().Contains(name.Trim()) &&
                !article.Summary.Trim().Contains(name.Trim()))
            {
                map.TryGetValue("NONE", out rate);
            }
            return rate;
        }

        public Model.Article CreateArticle(Model.Article article)
        {
            Int64 ID = articleDAL.InsertArticle(article.Title, article.FullName, article.PublishDate, article.ISSNNumber, article.CNNumber, article.CollectionNumber, article.Category, article.Summary);
            article.ID = ID;
            // 关键字列表
            foreach (Keyword keyword in article.KeyWord)
            {
                keyword.Article = article;
                // 针对文章的计数
                keyword.Count = 1;
                keyword.RightRate = Double.Parse(CalculateRate(keyword.Name, article).ToString());
                keyword.ID = keywordDAL.InsertKeyword(keyword.Name, keyword.Count, Decimal.Parse(keyword.RightRate.ToString()), ID, keyword.TotalCount);
            }
            // 插入机构列表
            foreach (Institution institution in article.Institution)
            {
                institution.Article = article;
                institution.ID = insitutionDAL.InsertInstitution(institution.Name, institution.Sequence, ID);
            }
            // 插入作者列表
            foreach (Author author in article.Author)
            {
                author.Article = article;
                Decimal rate = Decimal.Parse("0.0");
                this.fillDAL.GetAuthorRate().TryGetValue(author.Sequence,out rate);
                author.RightRate = Double.Parse(rate.ToString());
                author.ID = authorDAL.InsertAuthor(author.Name, author.Sequence, ID, Decimal.Parse(author.RightRate.ToString()), author.TotalCount);
            }
            // 作者机构关联表
            foreach (Author author in article.Author)
            {
                foreach (Institution institution in author.Institutions)
                {
                    if (author.Article != null && institution.Article != null)
                    {
                        if (author.Article.ID == institution.Article.ID && author.Sequence == institution.Sequence)
                        {
                            authorDAL.InsertRelationAI(author.ID, institution.ID);
                        }
                    }
                }
            }
            return article;
        }
        // 批量创建Article模型
        public Boolean CreateArticles(IList<Model.Article> articles)
        {
            Boolean result = false;
            try
            {
                foreach (Article article in articles)
                {
                    this.CreateArticle(article);
                }
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
    }
}
